package com.android.lovingpet.home.view;


import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.bean.Body;
import com.android.lovingpet.common.userinfo.UserInfoActivity;
import com.android.lovingpet.mine.myInformation.MyinformationActivity;
import com.android.lovingpet.mine.pet.view.PetActivity;
import com.bumptech.glide.Glide;
import com.android.lovingpet.R;
import com.android.lovingpet.common.userlist.UserListActivity;
import com.android.lovingpet.bean.Account;
import com.android.lovingpet.mine.contribution.ContributionActivity;
import com.android.lovingpet.mine.like.LikeActivity;
import com.android.lovingpet.mine.messagecenter.MessageCenterActivity;
import com.android.lovingpet.mine.setting.SettingActivity;
import com.android.lovingpet.util.Utility;

import de.hdodenhof.circleimageview.CircleImageView;
import com.android.lovingpet.home.IMineContract;
import com.android.lovingpet.home.MinePresenter;

public class MineFragment extends BaseFragment<IMineContract.View, MinePresenter> implements View.OnClickListener, IMineContract.View {

    private CircleImageView ivHead;
    private TextView tvLevel;
    private ImageView ivSex;
    private TextView tvAttendance;
    private IMineContract.Presenter presenter;

    @Override
    protected int setContentView() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void onCreateView(View view) {
        ivHead = view.findViewById(R.id.iv_head);
        tvLevel = view.findViewById(R.id.tv_level);
        ivSex = view.findViewById(R.id.iv_sex);
        tvAttendance = view.findViewById(R.id.tv_attendance);
        presenter = getPresenter();
        view.findViewById(R.id.iv_head).setOnClickListener(this);
        view.findViewById(R.id.ll_like).setOnClickListener(this);
        view.findViewById(R.id.ll_contribution).setOnClickListener(this);
        view.findViewById(R.id.ll_fans).setOnClickListener(this);
        view.findViewById(R.id.ll_attention).setOnClickListener(this);
        view.findViewById(R.id.ll_attendance).setOnClickListener(this);
        view.findViewById(R.id.ll_message).setOnClickListener(this);
        view.findViewById(R.id.ll_information).setOnClickListener(this);
        view.findViewById(R.id.tv_setting).setOnClickListener(this);
        view.findViewById(R.id.ll_pet).setOnClickListener(this);
        ((TextView) view.findViewById(R.id.tv_title)).setText("个人中心");
        setInfo();
    }

    @Override
    public MinePresenter createPresenter() {
        return new MinePresenter(this);
    }

    @Override
    public IMineContract.View createView() {
        return this;
    }

    @Override
    public void onResume() {
        super.onResume();
        setInfo();
        presenter.isSign();
        presenter.getFansAndAttentions();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_head:
                Intent info = new Intent(getContext(), UserInfoActivity.class);
                info.putExtra("account", Utility.getAccount());
                startActivity(info);
                break;

            case R.id.ll_like:
                startActivity(new Intent(getActivity(), LikeActivity.class));
                break;

            case R.id.ll_contribution:
                startActivity(new Intent(getActivity(), ContributionActivity.class));
                break;

            case R.id.ll_fans:
                Intent fans = new Intent(getActivity(), UserListActivity.class);
                fans.putExtra(UserListActivity.TITLE_KEY, UserListActivity.TITLE_FANS);
                fans.putIntegerArrayListExtra(UserListActivity.IDS_KEY, Utility.getAccount().getFans());
                startActivity(fans);
                break;

            case R.id.ll_attention:
                Intent attention = new Intent(getActivity(), UserListActivity.class);
                attention.putExtra(UserListActivity.TITLE_KEY, UserListActivity.TITLE_ATTENTIONS);
                attention.putIntegerArrayListExtra(UserListActivity.IDS_KEY, Utility.getAccount().getLikes());
                startActivity(attention);
                break;

            case R.id.ll_attendance:
                presenter.sign();
                break;

            case R.id.ll_message:
                startActivity(new Intent(getActivity(), MessageCenterActivity.class));
                break;

            case R.id.ll_information:
                startActivity(new Intent(getActivity(), MyinformationActivity.class));
                break;

            case R.id.tv_setting:
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.ll_pet:
                startActivity(new Intent(getActivity(), PetActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void showToast(final String meg) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getContext(), meg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void sign(final int days) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvAttendance.setText("已签到" + days + "天");
            }
        });
    }

    @Override
    public void addInfo(Body body) {

    }

    private void setInfo() {
        Account account = Utility.getAccount();
        Glide.with(this).load(account.getHead()).into(ivHead);
        tvLevel.setText(account.getGrade() + "");
        if (account.getGender() == 0) {
            ivSex.setImageDrawable(getResources().getDrawable(R.drawable.ic_woman));
        } else {
            ivSex.setImageDrawable(getResources().getDrawable(R.drawable.ic_man));
        }
    }
}
